import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;

public class Spots extends Applet {
   final	int MAX		= 20;
   int		xspots[]	= new int[MAX];
   int 		yspots[]	= new int[MAX];
   int 		currspots 	= 0;

   public void init() {
      setBackground(Color.white);
   }
   
   public boolean mouseDown(Event evt, int x, int y) {
	 
      if (currspots < MAX) {
    	 boolean isOverlapping=false;    	 
    	 
    	 for(int i = 0; i < currspots; i++) {
        	 
        	 if(yspots[i]==y&&xspots[i]==x)
    		 {
    			 isOverlapping=true;
    			 break;
    		 }
        	 
        	 //case 1 current spot: square- to be drawn: square 
        	 if(i%2==1 && currspots%2==1){
        		 
        		 //square to be drawn
        		 int yUpper	= y+25;
            	 int yLower	= y-25;
            	 int xLeft	= x-25;
            	 int xRight	= x+25;
            	 
            	 //current spot
            	 int ySpotLower	= yspots[i]-25;
            	 int ySpotUpper	= yspots[i]+25;
            	 int xSpotLeft	= xspots[i]-25;
            	 int xSpotRight	= xspots[i]+25;
            	 
            	 //check if there's an overlapping part on each side
	        	 if(((ySpotLower<=yUpper && yUpper<=ySpotUpper)||
	        		 (ySpotLower<=yLower && yLower<=ySpotUpper))&& 
	        		((xSpotLeft<=xRight && xRight<=xSpotRight)||
	        		 (xSpotLeft<=xLeft && xLeft<=xSpotRight))){
	    			 
	        		 isOverlapping=true;
	    			 break;
	    			 
	        	 }
        	 }
        	 //case 2 current spot:circle- to be drawn: circle
        	 else if(i%2==0&&currspots%2==0)
        	 {
        		 //calculate the distance between the centers of the 2 circles
        		 //if the distance is less than the sum of the radius of the circles then they will overlap 
        		 double distance= Math.sqrt(((y-yspots[i])*(y-yspots[i])) +((x-xspots[i])*(x-xspots[i])));
        		
        		 if(distance<50){
        			 
        			 isOverlapping=true;
	    			 break;
	    			 
        		 }
        		 
        	 }
        	 //case 3 
        	 else{
        		 
        		 int left=0,right=0,top=0,bottom=0;
        		 int xCenter=1, yCenter=0;
        		 
        		//square-circle
        		 if(i%2==1&&currspots%2==0){
        			 
        			 //sides of the square
        			 left	= xspots[i]-25;
        			 right	= xspots[i]+25;
        			 top	= yspots[i]-25;
        			 bottom	= yspots[i]+25;
        			 
        			 //center of the circle
        			 xCenter= x;
        			 yCenter= y;
        		 }
        		 
        		 //circle-square
        		 else{
        			 
        			 //sides of the square
        			 left	= x-25;
        			 right	= x+25;
        			 top	= y-25;
        			 bottom	= y+25;
        			 
        			 //center of the circle
        			 xCenter= xspots[i];
        			 yCenter= yspots[i];
        		 }
        		 
        		 //test each point in the boundary of the square if it's within the circle
    			 for(int j=0;j<=50;j++){
    				 
    				 if(((left+j)-xCenter)*((left+j)-xCenter)+(top-yCenter)*(top-yCenter)<=625 ||
    					((left-xCenter)*(left-xCenter)+((top+j)-yCenter)*((top+j)-yCenter))<=625 ||
    					(((left+j)-xCenter)*((left+j)-xCenter)+(bottom-yCenter)*(bottom-yCenter))<=625 ||
    					((right-xCenter)*(right-xCenter)+((top+j)-yCenter)*((top+j)-yCenter))<=625 ){
	    				 isOverlapping=true;
	    				 break;
	    			 }
    				 
        		 }
 
        	 }
    	 }
    	
    	 if(!isOverlapping){
    		 
    		 addspot(x, y);
    		 showStatus("");
    		 return true;
    		 
    	 }
    	
    	 else
    	 {
    		 
    		 System.out.println("Overlapping!");
             showStatus("Overlapping!");
    		 return false;
    		 
    	 }
      } 
      else {
    	  
         System.out.println("Too many spots!");
         showStatus("Too many spots!");
         return false;
         
      }
   }
   
   void addspot(int x, int y) {
	   
      xspots[currspots] = x;
      yspots[currspots] = y;
      currspots++;
      repaint();
      
   }

   public void paint(Graphics g) {
	   
      for(int i = 0; i < currspots; i++) {
    	  
         if(i%2==0){
        	 
        	 g.setColor(Color.BLACK);
			 g.fillOval(xspots[i]-25,yspots[i]-25,50,50);
			 
			 g.setColor(Color.MAGENTA);
			 g.drawArc(xspots[i],yspots[i]-50, 50, 50, 180, 90);
			 g.drawArc(xspots[i],yspots[i], 50, 50, 90, 90);
			 g.drawArc(xspots[i]-50,yspots[i], 50, 50, 0, 90);
			 g.drawArc(xspots[i]-50,yspots[i]-50, 50, 50, 270, 90);
			 
         }
         
         else{
        	 
        	 g.setColor(Color.BLACK);
     		 g.fillRect(xspots[i]-25,yspots[i]-25,50,50);
     		 
     		 g.setColor(Color.MAGENTA);
     		 g.drawArc(xspots[i],yspots[i]-25, 50, 50, 90, 180);
     		 g.drawArc(xspots[i]-25,yspots[i], 50, 50, 0, 180);
     		 g.drawArc(xspots[i]-50,yspots[i]-25,50,50, 270, 180);
     		 g.drawArc(xspots[i]-25, yspots[i]-50, 50, 50,180, 180);
         }  
      }
   }
}
